package com.example.a2032.fragment;

import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.MenuItem;
import android.view.View;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.annotation.StringRes;
import androidx.lifecycle.ViewModelProvider;
import androidx.swiperefreshlayout.widget.SwipeRefreshLayout;


import com.example.a2032.R;
import com.example.a2032.activity.BaseInitActivity;
import com.example.a2032.activity.ChatActivity;
import com.example.a2032.constant.DemoConstant;
import com.example.a2032.enums.Status;
import com.example.a2032.interfaceOrImplement.OnResourceParseCallback;
import com.example.a2032.net.Resource;
import com.example.a2032.viewmodel.ContactsViewModel;
import com.hyphenate.easeui.constants.EaseConstant;
import com.hyphenate.easeui.domain.EaseUser;
import com.hyphenate.easeui.interfaces.OnItemClickListener;
import com.hyphenate.easeui.model.EaseEvent;
import com.hyphenate.easeui.modules.contact.EaseContactListFragment;


import java.util.List;

public class ContactListFragment extends EaseContactListFragment implements SwipeRefreshLayout.OnRefreshListener {

    private ContactsViewModel mViewModel;

    @Override
    public void initView(Bundle savedInstanceState) {
        super.initView(savedInstanceState);

        //设置无数据时空白页面
        contactLayout.getContactList().getListAdapter().setEmptyLayoutResource(R.layout.demo_layout_friends_empty_list);
        contactLayout.showSimple();
        addHeader();

        //设置为简洁模式
        //contactLayout.showSimple();
        //获取列表控件
        //EaseContactListLayout contactList = contactLayout.getContactList();
        //设置条目高度
        //contactList.setItemHeight((int) EaseCommonUtils.dip2px(mContext, 80));
        //设置条目背景
        //contactList.setItemBackGround(ContextCompat.getDrawable(mContext, R.color.gray));
        //设置头像样式
        //contactList.setAvatarShapeType(2);
        //设置头像圆角
        //contactList.setAvatarRadius((int) EaseCommonUtils.dip2px(mContext, 5));
        //设置header背景
        //contactList.setHeaderBackGround(ContextCompat.getDrawable(mContext, R.color.white));
    }







    /**
     * 添加头布局
     */
    public void addHeader() {
        contactLayout.getContactList().addCustomItem(R.id.contact_header_item_group_list, R.drawable.em_friends_group_chat, getString(R.string.em_friends_group_chat));
    }

    @Override
    public void initListener() {
        super.initListener();
        contactLayout.getSwipeRefreshLayout().setOnRefreshListener(this);
//        contactLayout.getContactList().setOnCustomItemClickListener(new OnItemClickListener() {
//            @Override
//            public void onItemClick(View view, int position) {
//                EaseContactCustomBean item = contactLayout.getContactList().getCustomAdapter().getItem(position);
//                switch (item.getId()) {
//                    case R.id.contact_header_item_new_chat :
//                        AddContactActivity.startAction(mContext, SearchType.CHAT);
//                        break;
//                    case R.id.contact_header_item_group_list :
//                        GroupContactManageActivity.actionStart(mContext);
//                        break;
//                    case R.id.contact_header_item_chat_room_list :
//                        ChatRoomContactManageActivity.actionStart(mContext);
//                        break;
//                }
//            }
//        });
//
   }

    @Override
    public void initData() {
        mViewModel = new ViewModelProvider(this).get(ContactsViewModel.class);

        mViewModel.getContactObservable().observe(this, response -> {
            parseResource(response, new OnResourceParseCallback<List<EaseUser>>() {
                @Override
                public void onSuccess(List<EaseUser> data) {
                    contactLayout.getContactList().setData(data);
                }

                @Override
                public void onLoading(@Nullable List<EaseUser> data) {
                    super.onLoading(data);
                    contactLayout.getContactList().setData(data);
                }

            });

        });

        mViewModel.resultObservable().observe(this, response -> {
            parseResource(response, new OnResourceParseCallback<Boolean>() {
                @Override
                public void onSuccess(Boolean data) {
                    //showToast(R.string.em_friends_move_into_blacklist_success);
                    mViewModel.loadContactList();
                }
            });
        });

        mViewModel.deleteObservable().observe(this, response -> {
            parseResource(response, new OnResourceParseCallback<Boolean>() {
                @Override
                public void onSuccess(Boolean data) {
                    mViewModel.loadContactList();
                }
            });
        });

        mViewModel.messageChangeObservable().with(DemoConstant.CONTACT_CHANGE, EaseEvent.class).observe(this, event -> {
            if(event == null) {
                return;
            }
            if(event.isContactChange()) {
                mViewModel.loadContactList();
            }
        });

        mViewModel.loadContactList();
    }




    @Override
    public void onItemClick(View view, int position) {
        super.onItemClick(view, position);
        EaseUser item = contactLayout.getContactList().getItem(position);
        ChatActivity.actionStart(mContext, item.getUsername(), EaseConstant.CHATTYPE_SINGLE);
        Log.i(TAG, "onItemClick: test");
    }

    private static final String TAG = "ContactListFragment";
    /**
     * 解析Resource<T>
     * @param response
     * @param callback
     * @param <T>
     */
    public <T> void parseResource(Resource<T> response, @NonNull OnResourceParseCallback<T> callback) {
        if(response == null) {
            return;
        }
        if(response.status == Status.SUCCESS) {
            callback.hideLoading();
            callback.onSuccess(response.data);
        }else if(response.status == Status.ERROR) {
            callback.hideLoading();
            if(!callback.hideErrorMsg) {
                Log.i(TAG, "parseResource: "+response.getMessage());
                //showToast(response.getMessage());
            }
            callback.onError(response.errorCode, response.getMessage());
        }else if(response.status == Status.LOADING) {
            callback.onLoading(response.data);
        }
    }




    @Override
    public void onRefresh() {
        mViewModel.loadContactList();
    }
}
